package com.xc.file.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xc.file.config.Constants;
import com.xc.file.entity.FileEntity;
import com.xc.file.entity.UserInfoEntity;
import com.xc.file.enums.FailCode;
import com.xc.file.mapper.UserInfoMapper;
import com.xc.file.service.FileService;
import com.xc.file.service.UserInfoService;
import com.xc.tool.utils.Md5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;
import java.util.UUID;

/**
 * <p>空间流浪服务实现类</p>
 *
 * @author xc
 * @version v1.0.0
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfoEntity> implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private Constants constants;
    @Autowired
    private FileService fileService;

    @Override
    public void computeFreeFlow(String userId, long size, boolean isAdd) {
        synchronized ("") {
            //验证可用空间是否充足
            UserInfoEntity userInfoEntity = getUserInfoByUserId(userId);
            //计算可用空间
            if (isAdd) {
                long freeFlow = userInfoEntity.getFreeFlow() + size;
                userInfoEntity.setFreeFlow(freeFlow);
            } else {
                if (userInfoEntity.getFreeFlow() < size) {
                    throw FailCode.FREE_FLOW_SHORTAGE.getOperateException();
                }
                long freeFlow = userInfoEntity.getFreeFlow() - size;
                userInfoEntity.setFreeFlow(freeFlow);
            }
            if (!this.updateById(userInfoEntity)) {
                throw FailCode.UPDATE_FREE_FLOW_FAIL.getOperateException();
            }
        }
    }

    @Override
    public void createUserInfo(UserInfoEntity userInfoEntity) {
        userInfoEntity.setShareCode(Md5Utils.getSaltMd5(UUID.randomUUID().toString()));
        userInfoEntity.setFreeFlow(constants.getInitFreeFlow());
        userInfoEntity.setCloudSpace(constants.getInitCloudSpace());
        if (!this.save(userInfoEntity)) {
            throw FailCode.CREATE_USER_INFO_FAIL.getOperateException();
        }
    }

    @Override
    public Long getUseSpace(String userId) {
        FileEntity fileEntity = new FileEntity();
        fileEntity.setUserId(userId);
        QueryWrapper<FileEntity> queryWrapper = new QueryWrapper<>(fileEntity);
        queryWrapper.select("sum(size) as total");
        Map<String, Object> map = fileService.getMap(queryWrapper);
        long total = 0L;
        if (map != null) {
            total = ((BigDecimal) map.get("total")).longValue();
        }
        return total;
    }

    @Override
    public UserInfoEntity getUserInfoByAccount(String account) {
        UserInfoEntity userInfoEntity = new UserInfoEntity();
        userInfoEntity.setAccount(account);
        return userInfoMapper.selectOne(new QueryWrapper<>(userInfoEntity));
    }

    @Override
    public UserInfoEntity getUserInfoByUserId(String userId) {
        UserInfoEntity userInfoEntity = new UserInfoEntity();
        userInfoEntity.setUserId(userId);
        return userInfoMapper.selectOne(new QueryWrapper<>(userInfoEntity));
    }

    @Override
    public void verifyUserSpace(String userId, long size) {
        long total = getUseSpace(userId);
        UserInfoEntity userInfoEntity = getUserInfoByUserId(userId);
        // 验证网盘是否有空间可用
        if ((userInfoEntity.getCloudSpace() - total) < size) {
            throw FailCode.CLOUD_DISK_SPACE_SHORTAGE.getOperateException();
        }
    }
}
